package com.han.school.controller;

import com.github.pagehelper.PageInfo;
import com.han.school.pojo.Book;
import com.han.school.pojo.Iron;
import com.han.school.service.IronService;
import com.han.school.vo.BookVo;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import utils.HzdlJsonResult;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class IronController {
    @Autowired
    IronService ironService;
    @RequestMapping("ironur")
    @ResponseBody
    public String iron(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        System.out.println(originalFilename);
        File fil=new File("D:\\java web\\school1\\src\\main\\resources\\static\\images"+originalFilename);
        file.transferTo(fil);
        Integer addiron = ironService.addiron(originalFilename);
        if (addiron>0){
        return "1";}else {
            return "0";
        }

    }
    @RequestMapping("ironall")
    @ResponseBody
    public Map<String,Object> selectiron(Model model){
        ModelAndView modelAndView=new ModelAndView();
        List<Iron> select = ironService.select();
        Map<String,Object> map=new HashMap<>();
        map.put("data",select);
        return map;
    }

    @RequestMapping("bookadd")
    @ResponseBody
    public String addbook(Book book){
        Integer addbook = ironService.addbook(book);
        if (addbook==1){
            return "1";
        }else{
            return "0";
        }
    }
    //启用
    @RequestMapping("upcheck")
    @ResponseBody
    public String upb(BookVo bookVo){
        Integer addbook =ironService.upcheck(bookVo);
        if (addbook==1){
            return "1";
        }else{
            return "0";
        }
    }
    //禁用
    @RequestMapping("upcheck1")
    @ResponseBody
    public String upb1(BookVo bookVo){
        Integer addbook = ironService.upcheck1(bookVo);
        if (addbook==1){
            return "1";
        }else{
            return "0";
        }
    }
    //删除
    @RequestMapping("delss")
    @ResponseBody
    public String delss(BookVo bookVo){
        Integer addbook = ironService.delss(bookVo);
        if (addbook==1){
            return "1";
        }else{
            return "0";
        }
    }
    @RequestMapping("seleall")
    @ResponseBody
    public HzdlJsonResult seleall(BookVo bookVo){
        PageInfo<BookVo> seletall = ironService.seletall(bookVo);
        return HzdlJsonResult.ok(seletall.getList(),Long.valueOf(seletall.getTotal()).intValue());
    }
    @RequestMapping("updatebookonle")
    @ResponseBody
    public String updatebook(BookVo bookVo){
        Integer addbook = ironService.updatebook(bookVo);
        if (addbook==1){
            return "1";
        }else{
            return "0";
        }
    }
    @RequestMapping("deletebookonle")
    @ResponseBody
    public String deletebook(Book book){
        System.out.println(book);
        Integer addbook = ironService.deletebook(book);
        if (addbook==1){
            return "1";
        }else{
            return "0";
        }
    }

}
